using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace NL.Advanced
{

    public static class MulticastDelegateEX
    {
        public static void INVOKE_Safe<T>(this Action<T> w, T param)
        {
            if (w == null) return;


            foreach (var a in w.GetInvocationList())
            {
                Action<T> act = a as Action<T>;
                if (a.Target == null || a.Target.ToString() == "null")
                    w -= act;
                //try
                //{
                //    act.Invoke(param);
                //}
                //catch
                //{
                //    w -= act;
                //}
            }

            w?.Invoke(param);

        }
    }

    public static class SafeInvoke
    {
        public struct BlockItem
        {
            public Vector3 Pos;
            public int Dir;
            public int Type;
        }
        public class ListCompare<BlockItem> : IEqualityComparer<BlockItem>
        {
            bool IEqualityComparer<BlockItem>.Equals(BlockItem x, BlockItem y)
            {
                return false;
            }

            int IEqualityComparer<BlockItem>.GetHashCode(BlockItem obj)
            {
                return obj.GetHashCode();
            }
        }
        public static float Intersect<BlockItem>(List<BlockItem> list1, List<BlockItem> list2)
        {


            var list3 = list1.Intersect(list2, new ListCompare<BlockItem>());



            return 0;
        }
        public static void Invoke<T>(ref Action<T> w, T param)
        {
            if (w == null) return;



            foreach (var a in w.GetInvocationList())
            {
                Action<T> act = a as Action<T>;
                if (a.Target == null || a.Target.ToString() == "null")
                    w -= act;
                //try
                //{
                //    act.Invoke(param);
                //}
                //catch
                //{
                //    w -= act;
                //}
            }

            w?.Invoke(param);

        }
    }

}

